Ext.ns('ru.proburo.oms');
if(true == Ext.isAir)
{
	Ext.ns('console');
	/*console.log = console.debug = console.warn = console.info = console.dump = function(){};*/
	console.log   = air.trace;
	console.debug = function(msg){
		this.prevTime = this.prevTime || 0;
		var t = (new Date()).valueOf();
		air.trace('[+'+(t-this.prevTime)+'] '+msg);
		this.prevTime = t;
	}
	console.warn  = air.trace;
	console.info  = air.trace;
	console.dump  = function(obj, maxLvl, lvl)
	{
		var sep = '    ';
		var str = '';		
		
		maxLvl = maxLvl || 1;
		lvl    = lvl || 0;
		
		if(lvl < maxLvl)
		{
			var pad = Array(lvl+1).join(sep).replace('undefined', '');
			
			if(typeof(obj) === 'object') //Array/Hashes/Objects
			{
				str += "Object(\n";
				
				for(var i in obj)
				{
					if(false === obj.hasOwnProperty(i)){ continue; }
					
					var value = obj[i];
					
					if(typeof(value) == 'object') //If it is an array,
					{
						str += pad + sep + "'" + i + "' => " + console.dump(value, maxLvl, lvl+1);
					}
					else if(typeof(value) == 'function')
					{
						str += pad + sep + "'" + i + "' => function(){}\n";
					}
					else
					{
						str += pad + sep + "'" + i + "' => \"" + value + "\"\n";
					}
				}
				
				str += pad + ")\n";
			}
			else //Stings/Chars/Numbers etc.
			{
				str = "===>"+obj+"<===("+typeof(obj)+")";
			}
		}
		else
		{
			str = "\n";
		}
		
		return str;
	}
}
else if(true === Ext.isIE)
{
	console = {};
	console.log   = function(){};
	console.debug = function(){};
	console.warn  = function(){};
	console.info  = function(){};
}

Ext.override(Ext.data.JsonReader, {
	extractValues : function(data, items, len) {
		var f, values = {};
        for(var j = 0; j < len; j++){
            f = items[j];
            var v = this.ef[j](data);
            
			// bugfix (flash.Data object given)
			if(v && v.toUTCString){
				var val = v.valueOf();
				v = new Date(val > 0 ? val : 0);
			}
			
			values[f.name] = f.convert((v !== undefined) ? v : f.defaultValue, data);
        }
        return values;
    }
});
/**
 * bugfix: #Ext.Container.doLayout doesn`t return #this 
 */
Ext.override(Ext.Container, {
	doLayout: function(shallow, force){
        var rendered = this.rendered,
            forceLayout = force || this.forceLayout;
		
        if(this.collapsed || !this.canLayout()){
            this.deferLayout = this.deferLayout || !shallow;
            if(!forceLayout){
                return this;
            }
            shallow = shallow && !this.deferLayout;
        } else {
            delete this.deferLayout;
        }
        if(rendered && this.layout){
            this.layout.layout();
        }
        if(shallow !== true && this.items){
            var cs = this.items.items;
            for(var i = 0, len = cs.length; i < len; i++){
                var c = cs[i];
                if(c.doLayout){
                    c.doLayout(false, forceLayout);
                }
            }
        }
        if(rendered){
            this.onLayout(shallow, forceLayout);
        }
        // Initial layout completed
        this.hasLayout = true;
        delete this.forceLayout;
		
		return this;
    }
});

/**
 *
 */
Ext.util.Format.ruMoney = function(v)
{
	if(!v || isNaN(v))
	{
		v = 0;
	}
	
	v = (Math.round((v-0)*100))/100;
	v = (v == Math.floor(v)) ? v + ".00" : ((v*10 == Math.floor(v*10)) ? v + "0" : v);
	v = String(v);
	
	var ps = v.split('.'),
		whole = ps[0],
		sub = ps[1] ? '.'+ ps[1] : '.00',
		r = /(\d+)(\d{3})/;
	
	while (r.test(whole)) {
		whole = whole.replace(r, '$1' + ' ' + '$2');
	}
	v = whole + sub;
	
	return '' +  v;
}

/**
 *
 */
ru.proburo.oms.Bootstrap = function()
{
	return {
        
		// private
        _activators: [],
        
        /**
         * Main method
         */
        bootstrap: function()
        {
			Ext.QuickTips.init();
			Ext.form.Field.prototype.msgTarget = 'side';
			
			console.info('ru.proburo.oms.Bootstrap.bootstrap()');
			
			this.viewport = new ru.proburo.oms.components.Viewport();
			
            for(var i in this._activators)
			{
				if(false === this._activators.hasOwnProperty(i))
				{
					continue;
				}
				
				console.info("Loading module " + (Number(i)+1) + " of " + this._activators.length);
				
				var activator = this._activators[i];
				this._activators[i] = new (activator)();
				this._activators[i].start(this.viewport);
			}            
        },
		
		/**
         * Register component
         */
        register: function(activator)
		{
			this._activators.push(activator);
		}        
    };
}();

ru.proburo.oms.Bootstrap.reg = ru.proburo.oms.Bootstrap.register;

Ext.EventManager.onDocumentReady(ru.proburo.oms.Bootstrap.bootstrap, ru.proburo.oms.Bootstrap, true);